package com.ruikong.dazhi.service.impl.base;

import com.alibaba.fastjson.JSONArray;
import com.ruikong.dazhi.dao.base.PermissionMapper;
import com.ruikong.dazhi.dao.base.RolePermissionMapper;
import com.ruikong.dazhi.entity.base.Permission;
import com.ruikong.dazhi.entity.base.Role;
import com.ruikong.dazhi.entity.base.RolePermission;
import com.ruikong.dazhi.model.input.PermissionInput;
import com.ruikong.dazhi.model.output.PermissionOutput;
import com.ruikong.dazhi.model.output.PermissionTreeOutput;
import com.ruikong.dazhi.model.output.TreeMenuOutput;
import com.ruikong.dazhi.model.output.TreeNatureOutput;
import com.ruikong.dazhi.service.base.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: dailychemicalmes
 * @description:
 * @author: tybengine
 * @create: 2020-11-30 16:11
 **/
@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 获取菜单目录(两级目录)
     * @param
     * @return
     */
    public List<PermissionOutput> getPermissionsByPersonnelId(PermissionInput permissionInput){
        //一级目录
        List<Permission> permissions = permissionMapper.getListByPersonnelId(permissionInput.getPersonnelId());

        List<PermissionOutput> outputPermissions = new ArrayList<>();
        for (Permission item : permissions) {
            PermissionOutput outputPermission = new PermissionOutput();
            outputPermission.setPermission(item);
            List<Permission> permissionsChildes = permissionMapper.getListByParentIdAndPersonnelId(item.getPermissionId(), permissionInput.getPersonnelId());
            outputPermission.setPermissions(permissionsChildes);
            outputPermissions.add(outputPermission);
        }
        return outputPermissions;
    }

    /**
     * 获取菜单目录树
     * @return
     */
    public String getPermissionTree(){
        //一级目录
        List<Permission> permissions = permissionMapper.getAllFirst();

        List<PermissionTreeOutput> outputPermissions = new ArrayList<>();

        for (Permission item : permissions) {
            PermissionTreeOutput outputPermission = new PermissionTreeOutput();
            outputPermission.setText(item.getPermissionName());
            outputPermission.setId(item.getPermissionId());


            List<Permission> permissionsChildes = permissionMapper.getAllSecondByParentId(item.getPermissionId());
            List<PermissionTreeOutput.Node> nodes = new ArrayList<>();
            for (Permission itemChild : permissionsChildes) {
                PermissionTreeOutput.Node node = new PermissionTreeOutput.Node();
                node.setId(itemChild.getPermissionId());
                node.setText(itemChild.getPermissionName());
                nodes.add(node);
            }
            outputPermission.setNodes(nodes);
            outputPermissions.add(outputPermission);
        }
        String jsonStr = JSONArray.toJSON(outputPermissions).toString();
        String data = jsonStr.replace("\"nodes\":[],", "");
        return data;
    }

    /**
     * 通过角色Id获取角色权限关联表中的权限
     * @param role
     * @return
     */
    public List<Permission> getPermissionListByRoleId(Role role){
        return permissionMapper.getPermissionListByRoleId(role.getRoleId());
    }

    /**
     * 页面切换
     * @param role
     * @return
     */
    public List<TreeMenuOutput> getPermissionByRoleId(Role role){
        List<Permission> permissions = permissionMapper.getExistByRoleId(role.getRoleId());
        Integer first = 0;
        List<Permission> firstItems = permissions.stream().filter(x->first.equals(x.getParentId())).sorted(Comparator.comparing(Permission::getSort)).collect(Collectors.toList());
        List<TreeMenuOutput> outputs = new ArrayList<>();
        if(firstItems!=null && firstItems.size()>0){
            for(Permission item : firstItems){
                TreeMenuOutput output = new TreeMenuOutput();
                output.setId(item.getPermissionId());
                output.setpId(item.getParentId());
                output.setName(item.getPermissionName());
                outputs.add(output);
                showPermission(item.getPermissionId(), permissions, outputs);
            }
        }
        return outputs;
    }

    public void showPermission(Integer pId, List<Permission> permissions, List<TreeMenuOutput> outputs){
        List<Permission> nextPermissions =  permissions.stream().filter(x->pId.equals(x.getParentId())).sorted(Comparator.comparing(Permission::getSort)).collect(Collectors.toList());
        if(nextPermissions!=null && nextPermissions.size()>0){
            for(Permission item : nextPermissions){
                TreeMenuOutput output = new TreeMenuOutput();
                output.setId(item.getPermissionId());
                output.setpId(item.getParentId());
                output.setName(item.getPermissionName());
                outputs.add(output);
                showPermission(item.getPermissionId(), permissions, outputs);
            }
        }
    }

    /**
     * 弹窗显示tree树
     */
    public List<TreeNatureOutput> showModalTree(Role role){
        List<RolePermission> rolePermissionsList = rolePermissionMapper.getIdsByRoleId(role.getRoleId());
        List<Integer> idsList = rolePermissionsList.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
        if(idsList == null || idsList.size() == 0){
            idsList = new ArrayList<>();
            idsList.add(-1);
        }
        List<Permission> permissions = permissionMapper.getAllPermissions();
        Integer first = 0;
        List<Permission> firstItems = permissions.stream().filter(x->first.equals(x.getParentId())).sorted(Comparator.comparing(Permission::getSort)).collect(Collectors.toList());
        List<TreeNatureOutput> outputs = new ArrayList<>();
        if(firstItems!=null && firstItems.size()>0){
            for(Permission item : firstItems){
                TreeNatureOutput output = new TreeNatureOutput();
                output.setId(item.getPermissionId());
                output.setpId(item.getParentId());
                output.setName(item.getPermissionName());
                if(idsList.contains(item.getPermissionId())){
                    output.setChecked(true);
                } else {
                    output.setChecked(false);
                }
                outputs.add(output);
                showModalPermission(item.getPermissionId(), permissions, outputs, idsList);
            }
        }
        return outputs;
    }
    public void showModalPermission(Integer pId, List<Permission> permissions, List<TreeNatureOutput> outputs, List<Integer> idsList){
        List<Permission> nextPermissions =  permissions.stream().filter(x->pId.equals(x.getParentId())).sorted(Comparator.comparing(Permission::getSort)).collect(Collectors.toList());
        if(nextPermissions!=null && nextPermissions.size()>0){
            for(Permission item : nextPermissions){
                TreeNatureOutput output = new TreeNatureOutput();
                output.setId(item.getPermissionId());
                output.setpId(item.getParentId());
                output.setName(item.getPermissionName());
                if(idsList.contains(item.getPermissionId())){
                    output.setChecked(true);
                } else {
                    output.setChecked(false);
                }
                outputs.add(output);
                showModalPermission(item.getPermissionId(), permissions, outputs, idsList);
            }
        }
    }
}
